#ifndef ACTION_H
#define ACTION_H

template<typename T, typename U>
class OperatorApply {
	public:
		OperatorApply(T _a, U _b) : a(_a), b(_b) { }
		
		inline bool recognize(ParseStream* x) {
			int i = x->getPosition();//TODO restore last condition
			b.save();
			if (a.recognize(x)) {
				int j = x->getPosition();
				x->setPosition(i);
				b(x,i,j);
				x->setPosition(j);
				b.clear();
				return true;
			}
			else {
				x->setPosition(i);
				b.restore();
				return false;
			}
		}
		
		typedef OperatorApply<T, U> embed_t;
	private:
		typename T::embed_t a;
		U b;
};

template<typename T, typename U> 
OperatorApply<T,U> action(T const& a, U const& b) {
	return OperatorApply<T,U>(a,b);
}

template<typename T, typename U> 
OperatorApply<T,U> operator>(T const& a, U const& b) {
	return OperatorApply<T,U>(a,b);
}

#endif

